//
//  PageCache.hpp
//  高并发内存池
//
//  Created by 卜绎皓 on 2023/2/14.
//



#pragma once

#include "Common.hpp"
#include "ObjectPool.hpp"
#include "PageMap.hpp"

//单例模式
class PageCache
{
public:
    //提供一个全局访问点
    static PageCache* GetInstance()
    {
        return &_sInst;
    }
    
    //获取从对象到span的映射
    Span* MapObjectToSpan(void* obj);
    
    //
    void ReleaseSpanToPageCache(Span* span);
    
    Span* NewSpan(size_t k);

    std::mutex _pageMtx;
    
private:
    SpanList _spanLists[NPAGES];
    
    ObjectPool<Span> _spanPool;
    
    //std::unordered_map<PAGE_ID,Span*> _idSpanMap;
    TCMalloc_PageMap1<32 - PAGE_SHIFT> _idSpanMap;
    
    PageCache()
    {}
    
    PageCache(const PageCache&) = delete;
    
    static PageCache _sInst;
};
